import 'package:base_flutter/base_flutter.dart';
import 'package:flutter/src/widgets/framework.dart';
import 'package:ls_work/model/building_model.dart';
import 'package:ls_work/model/food/food_record_model.dart';
import 'package:ls_work/net/post_request.dart';

import '../../a_yyt/models/floor_list_entity.dart';
import '../../net/response/food/food_order_record_data.dart';
import '../../net/response/food/new_food_record_data.dart';

class FoodOrderRecordViewModel extends BaseListViewModel<NewFoodRecordData> {
  FoodOrderRecordViewModel(BuildContext context) : super(context);

  var model = FoodRecordModel();
  var buildModel = BuildingModel();
  var searchName = "";
  var useDate = "";
  PickerDataWithIdModel? useTime;
  List<FloorEntity> buildingList = [];
  FloorEntity? selectBuilding;
  FloorInfo? selectFloor;
  var _format = [yyyy,'-',mm,'-',dd];


  @override
  Future<List<NewFoodRecordData>> requestListData() async {
    if (buildingList.isEmpty) {
      await getBuildingList();
    }
    var data = await PostRequest.orderRecordList(
        page: page, rows: pageLength, elderHumanName: searchName,
    startCreateTime: startDateStr,endCreateTime: endDateStr,useDate: useDate,buildingId: selectBuilding?.iId,floorId: selectFloor?.iId);
    if ('0' == data['code'].toString()) {
      var list = ((data['data']['rows'] as List?)??[])
          .map((e) => NewFoodRecordData.fromJson(e))
          .toList();
      return list;
    } else {
      throw data['msg'];
    }
  }

  @override
  void onCreated() {
    super.onCreated();
    requestRefresh();
  }

  Future<void> getBuildingList() async {
    buildingList = await buildModel.getBuildingList(rows: 100);
  }

  Future<void> cancel(num id) async {
    showLoadingDialog(msg: '提交中...');
    try {
      var data = await model.cancelFood(id);
      hideDialog();
      requestRefresh();
    } catch (e) {
      showToast(NetErrorUtils.getNetError(e));
      hideDialog();
    }
  }

  void selectUseTime() {
    var items = [
      PickerDataWithIdModel(pickName: "早餐", pickId: "1"),
      PickerDataWithIdModel(pickName: "午餐", pickId: "2"),
      PickerDataWithIdModel(pickName: "晚餐", pickId: "3"),
    ];
    DataPickerView(context, items, onSubmitListener: (p1, p2, p3) {
      var item = items[p1];
      useTime = item;
      notifyListeners();
      requestRefresh();
    }).show();
  }

  void selectBuildingPick() {
    if (buildingList.isEmpty) {
      showToast("未获取到楼栋信息");
      return;
    }
    DataPickerView(context, buildingList, onSubmitListener: (p1, p2, p3) {
      selectBuilding = buildingList[p1];
      notifyListeners();
      requestRefresh();
    }).show();
  }

  void selectUseDate(){
    DatePicker.showDatePicker(context,onConfirm: (date){
      useDate = date?.toDateStr(_format)??"";
      notifyListeners();
      requestRefresh();
    });
  }

  void selectFloorPick() {
    if (selectBuilding == null) {
      showToast("请先选择楼栋");
      return;
    }

    if ((selectBuilding?.floorList ?? []).isEmpty) {
      showToast("该楼栋无楼层信息");
      return;
    }

    DataPickerView(context, selectBuilding?.floorList??[],
        onSubmitListener: (p1, p2, p3) {
      selectFloor = selectBuilding!.floorList![p1];
      notifyListeners();
      requestRefresh();
    }).show();
  }

  void reset() {
    useDate = "";
    useTime = null;
    selectBuilding = null;
    selectFloor = null;
    startDateStr = "";
    startDateTime = null;
    endDateStr = "";
    endDateTime = null;
    notifyListeners();
    requestRefresh();
  }
}
